package hrms.controller;

import hrms.entity.Department;
import hrms.entity.Result;
import hrms.service.DepartmentService;
import hrms.service.StaffService;
import hrms.util.DeptUtil;
import hrms.util.MergeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// 浏览器输入 http://localhost:8899/hrms/department/ 进入测试页面，有些功能效果在控制台输出。
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @Autowired
    StaffService staffService;

    @RequestMapping("/")
    public String departmentPage() {
        return "department";
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public Result selectAllDepartments(){
        List<Department> departments = departmentService.selectAllDepartments();
        Map<String, Object> map = new HashMap<>();
        map.put("departmentList", departments);
        return Result.success().addMap(map);
    }

    @PostMapping("/selectDepartmentById/{id}")
    @ResponseBody
    public Result selectDepartmentById(@PathVariable("id") Integer departmentId){
        Map<String, Object> map = new HashMap<>();
        List<Department> list = departmentService.selectDepartmentById(departmentId);
        map.put("list",list);
        return Result.success().addMap(map);
    }

    @GetMapping("/selectSingleDepartmentById")
    public Result selectSingleDepartmentById(String id){
        Department department = departmentService.selectSingleDepartmentById(id);
        if(department == null){
            return Result.fail().error("找不到该部门");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("department",department);
        return Result.success().addMap(map);
    }

    @PostMapping("/addDepartment")
    public Result addDepartment(@RequestBody DeptUtil deptUtil){
        System.out.println("Department-controller");
        if(departmentService.selectDepartmentByName(deptUtil.getDepartmentName()) != null){
            return Result.fail().error("部门已存在");
        }
        departmentService.addDepartment(deptUtil.getDepartmentName(),deptUtil.getManagerId());
        return Result.success();

    }

    @PostMapping("/updateDepartment")
    public Result updateDepartment(@RequestBody Department department){
        System.out.println(department);
        if(staffService.findById(department.getManager().getId()) == null){
            return Result.fail().error("员工不存在");
        }
        departmentService.renameDepartment(department);
        return Result.success();
    }

    @RequestMapping("/changeCurrentNum")
    @ResponseBody
    public void changeCurrentNum(@RequestParam("departmentId") Integer departmentId,
                                 @RequestParam("num") Integer num){
        departmentService.changeCurrentNum(departmentId,num);
    }

    @RequestMapping("/changeManager")
    @ResponseBody
    public void changeManager(@RequestParam("departmentId") Integer departmentId,
                              @RequestParam("managerId") Integer managerId) {
        departmentService.changeManager(departmentId,managerId);
    }

    @PostMapping("/mergeDepartment")
    public Result mergeDepartment(@RequestBody MergeUtil mergeUtil){
        if(departmentService.selectDepartmentByName(mergeUtil.getDept1()) == null ||
            departmentService.selectDepartmentByName(mergeUtil.getDept2()) == null){
            return Result.fail().error("找不到需要合并的部门");
        }
        System.out.println(mergeUtil);
        departmentService.mergeDepartment(mergeUtil);
        return Result.success();
    }

}
